﻿package Public;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 服务器数据统计类
 * @author yuanjianwei
 *
 */
public class ServerData
{
	/** 
	 新增玩家数
	 
	*/
	private static int newPlayerNum;

	/** 
	 新增玩家数
	 
	*/
	public static int getNewPlayerNum()
	{
		return newPlayerNum;
	}


	/** 
	 登录玩家数
	 
	*/
	private static int loginPlayerNum;
	/** 
	 登录玩家数
	 
	*/
	public static int getLoginPlayerNum()
	{
		return loginPlayerNum;
	}



	/** 
	 付费用户数
	 
	*/
	public static int getRechargePlayerNum()
	{
		return listRechargePlayer.size();
	}

	private static Object lockNewPlayerNum = new Object();
	/** 
	 添加新增用户
	 
	*/
	public static void AddNewPlayerNum()
	{
		synchronized (lockNewPlayerNum)
		{
			newPlayerNum++;
		}
	}

	/** 
	 清空新增用户
	 
	 @return 
	*/
	public static int ClearNewPlayerNum()
	{
		synchronized (lockNewPlayerNum)
		{
			int tempNum = newPlayerNum;
			newPlayerNum=0;
			return tempNum;
		}
	}

	private static Object lockLoginPlayerNum = new Object();

	/** 
	 添加登录玩家数
	 
	*/
	public static void AddLoginPlayerNum()
	{
		synchronized (lockLoginPlayerNum)
		{
			loginPlayerNum++;
		}
	}

	/** 
	 清空登录玩家数
	 
	 @return 
	*/
	public static int ClearLoginPlayerNum()
	{
		synchronized (lockLoginPlayerNum)
		{
			int tempNum = loginPlayerNum;
			loginPlayerNum = 0;
			return tempNum;
		}
	}

	private static Object lockRechargePlayerNum = new Object();
	/**
	 * 充值玩家列表
	 */
	private static ArrayList<Integer> listRechargePlayer=new ArrayList<Integer>();
	/** 
	 添加充值用户数
	 
	*/
	public static void AddRechargePlayerNum(Integer playerid)
	{
		synchronized (lockRechargePlayerNum)
		{
			if(!listRechargePlayer.contains(playerid))
			{
				listRechargePlayer.add(playerid);
			}
		}
	}

	/** 
	 清空充值用户数
	 
	 @return 
	*/
	public static int ClearRechargePlayerNum()
	{
		synchronized (lockRechargePlayerNum)
		{
			int tempNum = getRechargePlayerNum();
			listRechargePlayer.clear();
			return tempNum;
		}
	}
	
	private static Object lockListCostPlayer = new Object();
	/*
	 * 消费血石用户列表
	 */
	private static ArrayList<Integer> listCostPlayer=new ArrayList<Integer>();
	/**
	 * 增加消费血石用户
	*/
	public static void AddCostPlayerNum(Integer playerid)
	{
		synchronized (lockListCostPlayer)
		{
			if(!listCostPlayer.contains(playerid))
			{
				listCostPlayer.add(playerid);
			}
		}
	}

	/**
	 *  清空消费血石用户数
	 @return 
	*/
	public static int ClearCostPlayerNum()
	{
		synchronized (lockListCostPlayer)
		{
			int tempNum = listCostPlayer.size();
			listCostPlayer.clear();
			return tempNum;
		}
	}
	
	private static Object lockDicChannelMoney = new Object();
	/*
	 * 渠道充值
	 */
	private static HashMap<String, Integer> dicChannelMoney=new HashMap<String, Integer>();
	/**
	 * 增加渠道充值记录
	*/
	public static void AddChannelMoney(String channel, int money)
	{
		synchronized (lockDicChannelMoney)
		{
			if(dicChannelMoney.containsKey(channel))
			{
				dicChannelMoney.put(channel, dicChannelMoney.get(channel) + money);
			}
			else 
			{
				dicChannelMoney.put(channel, money);
			}
		}
	}

	/**
	 *  清空渠道充值记录
	 @return 
	*/
	public static String ClearChannelMoney()
	{
		synchronized (lockDicChannelMoney)
		{
			StringBuilder result = new StringBuilder();
			Iterator iter = dicChannelMoney.entrySet().iterator();
			while (iter.hasNext()) {
				java.util.Map.Entry entry = (java.util.Map.Entry) iter.next();
				String key = (String)entry.getKey();
				result.append(key);
				result.append(",");
				
				int val = (Integer)entry.getValue();
				result.append(val);
				result.append(";");
			}
			dicChannelMoney.clear();
			
			return result.toString();
		}
	}
}